<?php
	include "common.php";
	session_start();
	$_SESSION['logInCheck'] =0;
	$userName=$_POST['uname'];
	$userPass=$_POST['pass'];
	
	$loginSql ="SELECT * FROM user WHERE email='$userName' and password='$userPass'";
	$loginSqlResult=mysql_query($loginSql);
	
	$loginSuccess=mysql_num_rows($loginSqlResult);
	
	if ($loginSuccess == 1){
		
		$_SESSION['logInCheck'] =1;
		//collect office hour from text file
		$myFile = "officeTime.txt";
		$fh = fopen($myFile, 'r');
		$theData = fread($fh, filesize($myFile));
		fclose($fh);
		$theData;
		$time=explode(".",$theData);
		$hour=$time[0];
		$minuite=$time[1];
		//end collect office hour from text file
		$logedInUserData=mysql_fetch_array($loginSqlResult);
		
		$carryThis = $logedInUserData['e_id'];			
		$officeHour = $logedInUserData['office_hour'];
		$officeMinuite = $logedInUserData['office_minute'];
		$userRole = $logedInUserData['role'];
		$timezone_offset = 6; // for getting local time offset hrs
		$systemDate = gmdate('y-m-d', time()+$timezone_offset*60*60);
		$systemMonth = gmdate('m', time()+$timezone_offset*60*60);
		$systemYear = gmdate('y', time()+$timezone_offset*60*60);
		$_SESSION['checkDate'] = $systemDate;
		$checkLogedInsql = "SELECT * FROM attendance WHERE e_id='$carryThis' and date='$systemDate'"; 
		$checkLogedInsqlResult = mysql_query($checkLogedInsql);
		$checkLogedInSuccess = mysql_num_rows($checkLogedInsqlResult);
		 
		$systemTime = gmdate('H:i:s', time()+$timezone_offset*60*60); // converting to seconds
		$systemDay =  gmdate('D', time()+$timezone_offset*60*60);
		$systemHour = gmdate('H', time()+$timezone_offset*60*60);
		$systemMinuite = gmdate('i', time()+$timezone_offset*60*60);
		$updateInTheOfficeData = "UPDATE attendance SET inTheOffice='true' WHERE e_id='$carryThis' and date='$systemDate'";
		$updateInTheOfficeDataQuery = mysql_query($updateInTheOfficeData);
		$_SESSION['LastLoginDate'] = gmdate('d M', time()+$timezone_offset*60*60);
		$_SESSION['LastLoginTime'] = $systemTime;
		$_SESSION['LastLoginDay'] = $systemDay;
		
		$checkResult = mysql_fetch_assoc($checkLogedInsqlResult);
		$_SESSION['LastLogOut'] = $checkResult['out_time'];
		
		$mainNavigationSql = "SELECT * FROM mainnavigation WHERE Role = '$userRole'";
		$mainNavigationSqlResult = mysql_query($mainNavigationSql);
		$mainNavigationData = mysql_fetch_array($mainNavigationSqlResult);
		
		$_SESSION['userLink'] = $mainNavigationData['Link'];		
		
		$_SESSION['id'] = $carryThis;
		$_SESSION['role'] = $userRole;
		$_SESSION['email'] = $logedInUserData['email'];
		$_SESSION['userFname'] = $logedInUserData['f_name'];
		$_SESSION['userLname'] = $logedInUserData['l_name'];
		$_SESSION['supervisor'] = $logedInUserData['supervisor'];
		
		if($checkLogedInSuccess != 1){			
			$userSql = "SELECT * FROM apply_for_work_tonight WHERE e_id = '$carryThis' AND  status = 'ACCEPTED' AND '$systemDate' between work_from AND work_to";
			$userSqlQuery = mysql_query($userSql);
			 $userSqlQueryResult = mysql_num_rows($userSqlQuery);
			$userSqlQueryResult = mysql_num_rows($userSqlQuery);
			$userSqlQueryResult = mysql_num_rows($userSqlQuery);
			if ($userSqlQueryResult >0)
			{
				$updateSql = "UPDATE user SET office_hour = '2',office_minute = '0' WHERE e_id = '$carryThis'";
				$updateSqlQuery = mysql_query($updateSql);
			}	
			else
			{
				$updateSql = "UPDATE user SET office_hour = '$hour',office_minute = '$minuite' WHERE e_id = '$carryThis'";
				$updateSqlQuery = mysql_query($updateSql);
			}	
				
			if($systemHour < $officeHour && $systemHour !=0 ){//check valid hour if valid so 1
				$attendenceStatus = 1;
				$warning_update = "UPDATE user SET warning='0' WHERE e_id='$carryThis'";
				$warning_updateQuery = mysql_query($warning_update);				
			}
			else if($systemHour == $officeHour && $systemHour !=0 && $systemMinuite <= $officeMinuite){ //check valid hour with minuite
				$attendenceStatus = 1;
				$warning_update = "UPDATE user SET warning='0' WHERE e_id='$carryThis'";
				$warning_updateQuery = mysql_query($warning_update);				
			}
			else{ //is no valid so 0
				$attendenceStatus = 0;
				$catchTlateFromUserTable = "SELECT * FROM user WHERE e_id='$carryThis'";
				$catchTlateFromUserTableQuery = mysql_query($catchTlateFromUserTable);
				$catchTlateFromUserTableFetchArray = mysql_fetch_array($catchTlateFromUserTableQuery);				
				$collectTtableValue = $catchTlateFromUserTableFetchArray['t_late'];
				if($collectTtableValue<2){
					//No punishment just warning					
					$collectTtableValue++;
					$updateTheValueOfTlate = "UPDATE user SET t_late='$collectTtableValue' WHERE e_id='$carryThis'";
					$updateTheValueOfTlateQuery = mysql_query($updateTheValueOfTlate);
					$warning_update = "UPDATE user SET warning='1' WHERE e_id='$carryThis'";
					$warning_updateQuery = mysql_query($warning_update);
					//exit;
				}
				
				else{
					
					//Punishment Area for late come
					
					$updateTheValueOfTlate = "UPDATE user SET t_late='0' WHERE e_id='$carryThis'";
					$updateTheValueOfTlateQuery = mysql_query($updateTheValueOfTlate);
					$goingToTheEarnCasualLive = "SELECT * FROM earn_casual_leave WHERE e_id='$carryThis'";
					$goingToTheEarnCasualLiveQuery = mysql_query($goingToTheEarnCasualLive);
					$goingToTheEarnCasualLiveFetchArray = mysql_fetch_array($goingToTheEarnCasualLiveQuery);
					$collectDataTearnl = $goingToTheEarnCasualLiveFetchArray['t_earn_l'];
					$collectDataAvailableLeave = $goingToTheEarnCasualLiveFetchArray['available_leave'];
					$collectDataTcasuall = $goingToTheEarnCasualLiveFetchArray['t_casual_l'];
					$collectDataCasualAvailableLeave = $goingToTheEarnCasualLiveFetchArray['c_avail'];
					
					
					//respictively decreament the leave
					if($collectDataTcasuall>0){
						//Deducting one casual leave						
						$warning_update = "UPDATE user SET warning='2' WHERE e_id='$carryThis'";
						$warning_updateQuery = mysql_query($warning_update);										
						$collectDataTcasuall--;
						$collectDataCasualAvailableLeave--;
						$updateTheValueOfLeaveTable = "UPDATE earn_casual_leave SET t_casual_l='$collectDataTcasuall', c_avail='$collectDataCasualAvailableLeave' WHERE e_id='$carryThis'";
						$updateTheValueOfLeaveTableQuery = mysql_query($updateTheValueOfLeaveTable);
					}
					
					else if($collectDataTearnl>0){
						//Deducting one earn live
						$warning_update = "UPDATE user SET warning='3' WHERE e_id='$carryThis'";
						$warning_updateQuery = mysql_query($warning_update);												
						$collectDataTearnl--;
						$collectDataAvailableLeave--;
						$updateTheValueOfLeaveTable = "UPDATE earn_casual_leave SET t_earn_l='$collectDataTearnl', available_leave='$collectDataAvailableLeave' WHERE e_id='$carryThis'";
						$updateTheValueOfLeaveTableQuery = mysql_query($updateTheValueOfLeaveTable);
					}
				}//end punishment area else
			}//end not valid  time else
			
			//Insert data into attendance table
			$attendenceStatus; 
			$logedInDataInsert = "INSERT INTO  attendance (e_id, date, day, in_time, out_time, status, check_logged_in, inTheOffice) 
								VALUES ('$carryThis','$systemDate','$systemDay','$systemTime','','$attendenceStatus', '1', 'true')";
			//Insert data into reason_for_late table
			$reasonForLateDataInsert = "INSERT INTO reason_for_late(e_id, date, submit, status) VALUES('$carryThis','$systemDate','0','0')";
			mysql_query($logedInDataInsert);
			mysql_query($reasonForLateDataInsert);		
			
		}// CheckLogInSuccess if condition end
		header('Location:'. $_SESSION['userLink']);		
	}//logInsSuccess if ocdition end
	else{
		header('Location: index.php?loginInfo=false');
	}
?>